package simulation;

public class MaxIncreaseToKeepCitySkyline807 {
    public int maxIncreaseKeepingSkyline(int[][] grid) {
        int[] x = new int[grid.length];
        int[] y = new int[grid[0].length];
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                x[i] = Math.max(x[i],grid[i][j]);
            }
        }

        for (int j = 0; j < grid[0].length; j++) {
            for (int i = 0; i < grid.length; i++) {
                y[j] = Math.max(y[j],grid[i][j]);
            }
        }
        int result = 0;
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                result += Math.min(x[i],y[j])-grid[i][j];
            }
        }
        return result;
    }
}
